﻿
    using Maticsoft.BLL;
    using Maticsoft.DBUtility;
    using Maticsoft.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Web;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Utils;
    using Wuqi.Webdiyer;

    public partial class WebPage_Dingdan_Unpay : OrderBBasePage
    {
        public Maticsoft.BLL.Coupon coupbll = new Maticsoft.BLL.Coupon();
        public Maticsoft.Model.Coupon coupmodel = new Maticsoft.Model.Coupon();
        public Maticsoft.BLL.orderdetail detailbll = new Maticsoft.BLL.orderdetail();
        public List<Maticsoft.Model.orderdetail> detaillist = new List<Maticsoft.Model.orderdetail>();
        private Maticsoft.Model.Flow flow = new Maticsoft.Model.Flow();
        private Maticsoft.BLL.Flow flowbll = new Maticsoft.BLL.Flow();
        public Maticsoft.BLL.Order orderbll = new Maticsoft.BLL.Order();
        public Maticsoft.Model.Order ordermodel = new Maticsoft.Model.Order();
        protected string state;
        public Maticsoft.BLL.System sysbll = new Maticsoft.BLL.System();
        public Maticsoft.Model.System sysmodel = new Maticsoft.Model.System();
        public Maticsoft.BLL.Team teambll = new Maticsoft.BLL.Team();
        public Maticsoft.Model.Team teammodel = new Maticsoft.Model.Team();
        public string time;
        protected string type;
        protected string typeValue;
        public Maticsoft.BLL.UserInfo userbll = new Maticsoft.BLL.UserInfo();
        public Maticsoft.Model.UserInfo usermodel = new Maticsoft.Model.UserInfo();

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.getWeifu();
        }

        private void deleteOrder(int id)
        {
            new Maticsoft.BLL.Order().Delete(id);
        }

        public string getTime()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<option value=''></option>");
            for (int i = 0; i < 0x18; i++)
            {
                builder.Append(string.Concat(new object[] { "<option value='", i, "'>", i, "</option>" }));
            }
            return builder.ToString();
        }

        private void getWeifu()
        {
            string wherestr = " 1=1 and State='" + this.typeValue + "' ";
            if (this.uname.Value != "")
            {
                wherestr = wherestr + "and username like '" + this.uname.Value + "%'";
            }
            if (this.uemail.Value != "")
            {
                wherestr = wherestr + " and Email like '" + this.uemail.Value + "%'";
            }
            if (this.team_id.Value != "")
            {
                wherestr = wherestr + " and Team_id=" + this.team_id.Value;
            }
            string str4 = wherestr;
            wherestr = str4 + " and Create_time between '" + this.time + " 00:00:00' and '" + this.time + " 23:59:59'";
            wherestr = "(" + wherestr + ")or( create_time between '" + this.time + " 00:00:00' and '" + this.time + " 23:59:59' and pay_time>'" + this.time + " 23:59:59')";
            StringBuilder builder = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            builder.Append("<tr >");
            builder.Append("<th width='40'>ID</th>");
            builder.Append("<th class='xiangmu'>项目</th>");
            builder.Append("<th width='70'>用户</th>");
            builder.Append("<th width='30' nowrap>数量</th>");
            builder.Append("<th width='50' nowrap>总款</th>");
            builder.Append("<th width='50' nowrap>余付</th>");
            builder.Append("<th width='50' nowrap>支付</th>");
            builder.Append("<th width='30' nowrap>操作</th>");
            builder.Append("</tr>");
            Maticsoft.Model.Team model = new Maticsoft.Model.Team();
            Maticsoft.BLL.Team team2 = new Maticsoft.BLL.Team();
            Maticsoft.Model.UserInfo info = new Maticsoft.Model.UserInfo();
            Maticsoft.BLL.UserInfo info2 = new Maticsoft.BLL.UserInfo();
            Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
            Maticsoft.Model.Order order2 = new Maticsoft.Model.Order();
            string tablename = " (SELECT  orderdetail.Num, orderdetail.Teamid, orderdetail.Teamprice, Team.Title, Team_1.Title AS team_title, [Order].*, [User].Email,[User].Username FROM Team RIGHT OUTER JOIN [Order] LEFT OUTER JOIN [User] ON [Order].User_id = [User].Id ON Team.Id = [Order].Team_id LEFT OUTER JOIN Team AS Team_1 INNER JOIN orderdetail ON Team_1.Id = orderdetail.Teamid ON [Order].Id = orderdetail.Order_id) ttt1 ";
            Pagers pagers = DbHelperSQL.SelectByPager("*", wherestr, " Id desc, pay_time desc ", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, tablename, "", "");
            if (pagers.MyData.Rows.Count > 0)
            {
                int num = 0;
                int num2 = 0;
                for (int i = 0; i < pagers.MyData.Rows.Count; i++)
                {
                    DataRow row = pagers.MyData.Rows[i];
                    if (Convert.ToInt32(row["Id"].ToString()) != num2)
                    {
                        if ((num % 2) != 0)
                        {
                            builder2.Append("<tr class='alt'>");
                        }
                        else
                        {
                            builder2.Append("<tr>");
                        }
                        builder2.Append("<td>" + row["Id"].ToString() + "</td>");
                        num2 = Convert.ToInt32(row["Id"].ToString());
                        model = team2.GetModel(int.Parse(row["Team_id"].ToString()));
                        if (model == null)
                        {
                            this.detaillist = this.detailbll.Searchorderdetail(Convert.ToInt32(row["Id"].ToString()));
                            builder2.Append("<td>&nbsp;");
                            int num4 = 0;
                            foreach (Maticsoft.Model.orderdetail orderdetail in this.detaillist)
                            {
                                num4++;
                                builder2.Append(string.Concat(new object[] { "(<a class='deal-title' href='../team.aspx?id=", orderdetail.Teamid, "' target='_blank'>", num4, ":" }));
                                builder2.Append((this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)) == null) ? "" : this.teambll.GetModel(Convert.ToInt32(orderdetail.Teamid)).Title);
                                builder2.Append("</a>)<br>");
                            }
                            builder2.Append("</td>");
                        }
                        else
                        {
                            builder2.Append(string.Concat(new object[] { "<td>", row["Team_id"], "&nbsp;(<a class='deal-title' href='/team.aspx?id=", row["Team_id"].ToString(), "' target='_blank'>", model.Title, "</a>)</td>" }));
                        }
                        info = info2.GetModel(int.Parse(row["User_id"].ToString()));
                        if (info != null)
                        {
                            builder2.Append(string.Concat(new object[] { "<td><a href='/ajax/manage.aspx?action=userview&id=", row["User_id"], "' class='ajaxlink'>", info.Email, "<br/>", info.Username, "</a>&nbsp;&raquo;&nbsp;<a href='/ajax/misc.aspx?action=sms&v=", info.Mobile, "' class='ajaxlink'>短信</a></td>" }));
                        }
                        else
                        {
                            builder2.Append("<td>&nbsp;</td>");
                        }
                        order2 = order.GetModel(int.Parse(row["Id"].ToString()));
                        builder2.Append("<td>" + row["Quantity"] + "</td>");
                        string currency = "\x00a5";
                        if (base.ASSystem != null)
                        {
                            currency = base.ASSystem.Currency;
                        }
                        builder2.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", row["Origin"], "</td>" }));
                        builder2.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", row["Credit"], "</td>" }));
                        builder2.Append(string.Concat(new object[] { "<td><span class='money'>", currency, "</span>", row["Money"], "</td>" }));
                        builder2.Append(string.Concat(new object[] { "<td class='op' nowrap><a ask='确定本单为现金付款?' href='Dingdan_Unpay.aspx?id=", row["Id"], "'>现金</a>｜<a href='Dingdan_Unpay.aspx?delete=", row["Id"], "' class=''>删除</a></td>" }));
                        builder2.Append("</tr>");
                        num++;
                    }
                }
            }
            this.AspNetPager1.RecordCount = pagers.TotalRecords;
            this.Literal1.Text = builder.ToString();
            this.Literal2.Text = builder2.ToString();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (base.Request["id"] != null)
            {
                base.SetSuccess("修改成功！");
                this.UpdadteFlow(Convert.ToInt32(base.Request["id"].ToString()));
                this.Updateorder(Convert.ToInt32(base.Request["id"].ToString()));
            }
            if (base.Request.QueryString["delete"] != null)
            {
                this.deleteOrder(int.Parse(base.Request.QueryString["delete"].ToString()));
                base.SetSuccess("删除成功！");
            }
            if ((((base.Request.QueryString["state"] != null) && (base.Request.QueryString["time"] != null)) && (base.Request.QueryString["state"].ToString() != "")) && (base.Request.QueryString["time"].ToString() != ""))
            {
                if (base.Request.QueryString["state"].ToString() == "pay")
                {
                    this.type = "已付款订单";
                }
                else
                {
                    this.type = "未付款订单";
                }
                this.Session.Add("type", this.type);
                this.Session.Add("time", base.Request.QueryString["time"].ToString());
                this.Session.Add("typeValue", base.Request.QueryString["state"].ToString());
            }
            if (((this.Session["time"] != null) && (this.Session["typeValue"] != null)) && (this.Session["type"] != null))
            {
                this.time = this.Session["time"].ToString();
                this.typeValue = this.Session["typeValue"].ToString();
                this.type = this.Session["type"].ToString();
            }
            this.HeaderControl1.Select = "order";
            this.getWeifu();
        }

        public void UpdadteFlow(int id)
        {
            this.ordermodel = this.orderbll.GetModel(id);
            if (HttpContext.Current.User.Identity.Name != null)
            {
                this.flow.User_id = this.ordermodel.User_id;
                this.flow.Admin_id = this.userbll.Getuserid(HttpContext.Current.User.Identity.Name.ToString());
                this.flow.Detail_id = id.ToString();
                this.flow.Direction = "expense";
                this.flow.Money = this.ordermodel.Origin;
                this.flow.Action = "cash";
                this.flow.Create_time = DateTime.Now;
                this.flowbll.Add(this.flow);
            }
            else
            {
                this.SetError("支付失败,请重新登录");
            }
        }

        public void Updateorder(int id)
        {
            this.ordermodel = this.orderbll.GetModel(id);
            this.teammodel = this.teambll.GetModel(this.ordermodel.Team_id);
            this.usermodel = this.userbll.GetModel(this.ordermodel.User_id);
            if ((this.teammodel != null) && (this.teammodel.Delivery == "coupon"))
            {
                for (int i = 0; i < this.ordermodel.Quantity; i++)
                {
                    this.coupmodel.Team_id = this.ordermodel.Team_id;
                    this.coupmodel.Order_id = id;
                    this.coupmodel.Type = "consume";
                    this.coupmodel.Credit = this.teammodel.Credit;
                    this.coupmodel.Consume = "N";
                    this.coupmodel.User_id = this.ordermodel.User_id;
                    this.coupmodel.Create_time = DateTime.Now;
                    this.coupmodel.Expire_time = this.teammodel.Expire_time;
                    this.coupmodel.Partner_id = this.teammodel.Partner_id;
                    this.coupmodel.Id = Helper.GetRandomString(12);
                    this.coupmodel.Secret = Utility.GetCouponpass();
                    this.coupbll.Add(this.coupmodel);
                    this.sysmodel = this.sysbll.GetModelByCache(1);
                    string sitename = "";
                    if (this.sysmodel != null)
                    {
                        sitename = this.sysmodel.sitename;
                    }
                    List<string> mobiles = new List<string>();
                    mobiles.Add(this.usermodel.Mobile);
                    string content = sitename + "项目：" + this.teammodel.Product + "，编号：" + this.coupmodel.Id + "，密码：" + this.coupmodel.Secret + "，有效期至：" + this.coupmodel.Expire_time.ToString("yyyy年MM月dd日");
                    Maticsoft.BLL.Order.SendSMS(mobiles, content);
                }
            }
            this.ordermodel.State = "pay";
            this.ordermodel.Service = "cash";
            this.ordermodel.Admin_id = base.AsUser.Id;
            this.ordermodel.Pay_time = new DateTime?(DateTime.Now);
            this.orderbll.Update(this.ordermodel);
        }
    }



